//
//  Exrension+NSDate.swift
//  weibo
//
//  Created by 张城 on 16/7/26.
//  Copyright © 2016年 张城. All rights reserved.
//

import UIKit

let df = NSDateFormatter()
let calendar = NSCalendar.currentCalendar()

let wes:String = String()
var aaa: String?


extension NSDate {
    
    
    class func sinaDate(created_at: String?)-> NSDate{
        
        df.locale  = NSLocale(localeIdentifier: "en_US")
        df.dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
        
        return df.dateFromString(created_at!)!
    }
    
    
    var sinaTimeStr: String {
        
        if isThisYear(self) {
            if calendar.isDateInToday(self) {
                
                let sec = Int(NSDate().timeIntervalSinceDate(self))
                
                if sec < 60 {
                    df.dateFormat = "刚刚"
                }else if sec < 3600 {
                    
                    
                    
                    df.dateFormat = "\(sec/60)分钟前"
                }else{
                    
                    df.dateFormat = "\(sec/3600)小时前"
                }
        
                
                
                
            }else if calendar.isDateInYesterday(self) {
                
                df.dateFormat = "昨天 HH:mm:ss"
                
                
            }else{
                
                
                df.dateFormat = "MM-dd HH:mm:ss"
                
                
                
            }
            
            
            
            
            
        }else {
        
        
        df.dateFormat = "yyyy-MM-dd HH:mm:ss"
        
        }
        
        
        return df.stringFromDate(self)
        
        
    }
    
    func isThisYear(creatDate: NSDate) -> Bool {
        
        
        df.dateFormat = "yyyy"
        
        
        let date = NSDate()
        
        let nowStr = df.stringFromDate(date)
        
        let creatStr = df.stringFromDate(creatDate)
        
        return nowStr == creatStr
        
    }
    
    
    
    
}

